﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using RemotingObject;

namespace ClientCommonClass
{
    /// <summary>
    /// 远程监控客户端操作
    /// </summary>
    public class ClientMonitorOperation
    {
        #region Private Field

        #region Static
        private static ClientMonitorOperation _instance = null;
        private static readonly object _padlock = new Object();
        #endregion

        private TcpServerChannel _channel;
        private RemotingMonitor _monitor;

        #endregion

        #region 构造函数

        /// <summary>
        /// 私有构造函数
        /// </summary>
        private ClientMonitorOperation()
        {
            try
            {
                _channel = new TcpServerChannel(9452);
            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        #endregion

        #region 属性

        /// <summary>
        /// 单例实体
        /// </summary>
        public static ClientMonitorOperation Instance
        {
            get
            {
                lock (_padlock)
                {
                    if (_instance == null)
                    {
                        _instance = new ClientMonitorOperation();
                    }
                    return _instance;
                }
            }
        }

        #endregion

        #region 公开的方法

        #region 注册
        /// <summary>
        /// 注册 TCP 信道
        /// </summary>
        public void RegisterChannel()
        {
            ChannelServices.RegisterChannel(_channel, false);
            //RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingMonitor), "MonitorServerUrl", WellKnownObjectMode.SingleCall);
            
            _monitor = new RemotingMonitor();
            ObjRef objRef = RemotingServices.Marshal(_monitor, "MonitorServerUrl"); // 客户端与服务器端为同一个实例
        }

        /// <summary>
        /// 注册发送消息事件
        /// </summary>
        /// <param name="sendMessageEvent">发送消息事件</param>
        public void RegisterSendMessageEvent(RemotingObject.SendMessageHandler sendMessageEvent)
        {
            if(_monitor != null && sendMessageEvent != null)
                _monitor.SendMessageEvent += sendMessageEvent;
        }

        /// <summary>
        /// 注册关机事件
        /// </summary>
        /// <param name="shutDownPCEvent">关机事件</param>
        public void RegisterShutDownPCEvent(RemotingObject.CommonHandler shutDownPCEvent)
        {
            if (_monitor != null && shutDownPCEvent != null)
                _monitor.ShutDownPCEvent += shutDownPCEvent;
        }

        /// <summary>
        /// 注册锁屏事件
        /// </summary>
        /// <param name="lockPCEvent">锁屏事件</param>
        public void RegisterLockPCEvent(RemotingObject.CommonHandler lockPCEvent)
        {
            if (_monitor != null && lockPCEvent != null)
                _monitor.LockPCEvent += lockPCEvent;
        }

        /// <summary>
        /// 注册解屏事件
        /// </summary>
        /// <param name="unlockPCEvent">解屏事件</param>
        public void RegisterUnlockPCEvent(RemotingObject.CommonHandler unlockPCEvent)
        {
            if (_monitor != null && unlockPCEvent != null)
                _monitor.UnlockPCEvent += unlockPCEvent;
        }
        #endregion

        #endregion
    }
}
